package com.itheima.day15.TCP.test7;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class ServerDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10008);


        while (true) {
            Socket accept = ss.accept();

            // 接收网络信息，保存到本地
            InputStream is = accept.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("backBurner\\a\\" + UUID.randomUUID().toString().replace("-","") + ".txt"));
            byte[] bytes = new byte[1024];
            int len;
            while ((len = bis.read(bytes)) != -1){
                bos.write(bytes,0,len);
            }

            // 给客户端响应
            OutputStream os = accept.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
            bw.write("保存成功！");
            bw.newLine();
            bw.flush();

            // 释放资源
            bos.close();
            accept.close();
            //ss.close();
        }
    }
}
